﻿using System;
using System.Web;
using System.Web.SessionState;

using ns_User;

namespace ns_SessionManage
{
    public static class c_SessionManage
    {
        private const String USER_NAME = "USER";
        private const Int32 SESSION_TIMEOUT = 1;//单位为分钟

        private static Boolean Put(String keyword, Object obj)
        {
            try
            {
                HttpContext.Current.Session[keyword] = obj;
                HttpContext.Current.Session.Timeout = SESSION_TIMEOUT;
                return true;
            }
            catch (Exception e)
            { return false; }
        }
        private static T Get<T>(String keyword)
        {
            return (T)HttpContext.Current.Session[keyword];
        }
        private static Boolean Del(String keyword)
        {
            try
            {
                HttpContext.Current.Session.Remove(keyword);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public static Boolean SaveCurrentUser(c_User user)//保存登录用户
        {
            try
            {
                Put(USER_NAME, user);
                return true;
            }
            catch (Exception e)
            { return false; }
        }
        public static c_User GetCurrentUser()//获取用户变量
        {
            try
            {
                return Get<c_User>(USER_NAME);
            }
            catch (Exception e)
            { return null; }
        }
        public static Boolean DelCurrentUser()
        {
            try
            {
                Del(USER_NAME);
                return true;
            }
            catch (Exception e)
            { return false ; }
        }
    }
}